﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace YXShop.Web.admin.product
{
    public partial class productparameter_edit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string classValue = ChangeHope.WebPage.PageRequest.GetString("classValue");
                if (classValue != "")
                {
                    ViewState["ClassValue"] = classValue;
                }
                int productTypeId = ChangeHope.WebPage.PageRequest.GetInt("TId");
                if (productTypeId > 0)
                {
                    ViewState["ProductTypeId"] = productTypeId;
                }
                int parameterId = ChangeHope.WebPage.PageRequest.GetInt("id");
                if (parameterId > 0)
                {
                    this.BindInfo(parameterId, productTypeId);
                }
                this.HyperLink1.NavigateUrl = "productparameter_list.aspx?classValue=" + ViewState["ClassValue"] + "&id=" + ViewState["ProductTypeId"];
                this.HyperLink2.NavigateUrl = "productparameter_list.aspx?classValue=" + ViewState["ClassValue"] + "&id=" + ViewState["ProductTypeId"];
                InitWebControl();
            }
        }
        /// <summary>
        /// 验证
        /// </summary>
        private void InitWebControl()
        {
            ChangeHope.WebPage.WebControl.Validate(this.txtName, "输入商品名称的名称,您可以对商品进行品牌归类", "isnull", "必填", "该项为必填项");
            ChangeHope.WebPage.WebControl.Validate(this.txtSort, "输入数字将作为显示的排列顺序", "isint", "必填", "该项为必填项");
            this.Form.Attributes.Add("onsubmit", "return CheckForm()");
        }
        #region 保存参数组
        protected void butSave_Click(object sender, EventArgs e)
        {
            YXShop.BLL.Product.ProductType bll = new YXShop.BLL.Product.ProductType();
            YXShop.Model.Product.ProductTypeParameter model = new YXShop.Model.Product.ProductTypeParameter();
            model.ParameterName = this.txtName.Text.Trim();
            model.ParameterType = 0;
            model.ParameterValue = "";
            model.Sort = Convert.ToInt32(this.txtSort.Text);
            model.TypeID = Convert.ToInt32(ViewState["ProductTypeId"]);
            model.ParID = 0;

            if (ViewState["ID"] == null)
            {
                bll.AddProductParameter(model);
                ChangeHope.WebPage.BasePage.PageRight("添加成功，您还可以继续添加。", "productparameter_edit.aspx?classValue=" + ViewState["ClassValue"] + "&TId=" + ViewState["ProductTypeId"]);
                
            }
            else
            {
                model.ID = Convert.ToInt32(ViewState["ID"]);
                if (bll.UpdateProductParameter(model, Convert.ToInt32(ViewState["ProductTypeId"])) > 0)
                {
                    ChangeHope.WebPage.BasePage.PageRight("修改成功。", "productparameter_list.aspx?classValue=" + ViewState["ClassValue"] + "&id=" + ViewState["ProductTypeId"]);
                }
                else
                {
                    ChangeHope.WebPage.BasePage.PageError("修改失败。", "productparameter_list.aspx?classValue=" + ViewState["ClassValue"] + "&id=" + ViewState["ProductTypeId"]);
                }
            }
        }
        #endregion

        #region 绑定参数信息
        protected void BindInfo(int id,int productTypeId)
        {
            YXShop.BLL.Product.ProductType bll = new YXShop.BLL.Product.ProductType();
            YXShop.Model.Product.ProductTypeParameter model = bll.GetProductParameterModel(id, productTypeId);
            if (model != null)
            {
                this.txtName.Text = model.ParameterName;
                this.txtSort.Text = model.Sort.ToString();
                ViewState["ID"] = model.ID;
            }

        }
        #endregion


    }
}
